package com.meishu.sdkdemo.adactivity.fullscreenvideo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.meishu.sdk.core.AdSdk;
import com.meishu.sdk.core.ad.fullscreenvideo.FullScreenVideoAd;
import com.meishu.sdk.core.ad.fullscreenvideo.FullScreenVideoAdListener;
import com.meishu.sdk.core.ad.fullscreenvideo.FullScreenVideoAdLoader;
//import com.meishu.sdk.core.ad.fullscreenvideo.IFullScreenMediaListener;
import com.meishu.sdk.core.ad.fullscreenvideo.IFullScreenVideoAd;
import com.meishu.sdk.core.loader.AdPlatformError;
import com.meishu.sdk.core.loader.InteractionListener;
import com.meishu.sdkdemo.R;
import com.meishu.sdkdemo.adid.IdProviderFactory;

public class FullScreenVideoActivity extends Activity implements FullScreenVideoAdListener {

    private final static String TAG = "FullScreenVideoActivity";
    private FullScreenVideoAdLoader fullScreenVideoAdLoader;
    private IFullScreenVideoAd fullScreenVideoAd;

    private Button btnLoad;
    private Button btnShow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_video);

        btnLoad = findViewById(R.id.loadFullScreenVideoAd);
        btnShow = findViewById(R.id.showFullScreenVideoAd);
        EditText editPid = findViewById(R.id.alternativeFullScreenVideoAdPlaceID);
        editPid.setText(IdProviderFactory.getDefaultProvider().fullScreenVideo());

        btnLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnShow.setEnabled(false);
                if (fullScreenVideoAdLoader != null) {
                    fullScreenVideoAdLoader.destroy();
                }
                fullScreenVideoAdLoader = new FullScreenVideoAdLoader(FullScreenVideoActivity.this, editPid.getText().toString(), FullScreenVideoActivity.this);
                fullScreenVideoAdLoader.loadAd();
            }
        });
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fullScreenVideoAd != null) {
                    fullScreenVideoAd.showAd();
                }
            }
        });
    }

    /**
     * 注意：广告加载成功不要再做原有逻辑处理，应该转移到onAdReady中
     * 广告加载成功，此时广告不一定会渲染成功，这里不做任何处理，所有处理统一放到onAdReady中
     */
    @Override
    public void onAdLoaded(IFullScreenVideoAd fullScreenVideoAd) {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
//        if (fullScreenVideoAd.getData() !=null){
//            Toast.makeText(this,fullScreenVideoAd.getData().toString(),Toast.LENGTH_LONG).show();
//            AdSdk.adConfig().userDenyCats(fullScreenVideoAd.getData().getCat());
//            AdSdk.adConfig().userDenyCids(fullScreenVideoAd.getData().getCid());
//            AdSdk.adConfig().userDenyAderIds(fullScreenVideoAd.getData().getAderId());
//        }



//        fullScreenVideoAd.setMediaListener(new IFullScreenMediaListener() {
//            @Override
//            public void onVideoCompleted() {
//                Log.e(TAG, "onVideoCompleted: " );
//            }
//
//            @Override
//            public void onSkippedVideo() {
//                Log.e(TAG, "onSkippedVideo: " );
//            }
//        });

    }

    @Override
    public void onAdError() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdPlatformError(AdPlatformError e) {

    }

    /**
     * 广告已经准备完毕，此时可以进行广告的展示操作和相关逻辑处理
     */
    @Override
    public void onAdReady(IFullScreenVideoAd fullScreenVideoAd) {
        btnShow.setEnabled(true);
        this.fullScreenVideoAd = fullScreenVideoAd;
        fullScreenVideoAd.setInteractionListener(new InteractionListener() {
            @Override
            public void onAdClicked() {
                Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
            }
        });
    }

    @Override
    public void onAdRenderFail(String msg, int code) {

    }

    public void onRenderFail(String msg, int code) {

    }

    public void onRenderSuccess(IFullScreenVideoAd iFullScreenVideoAd) {

    }

    @Override
    public void onAdExposure() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    public void onAdClosed() {
        Log.d(TAG, "DEMO ADEVENT " + (new Throwable().getStackTrace()[0].getMethodName()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (fullScreenVideoAdLoader != null) {
            fullScreenVideoAdLoader.destroy();
        }
    }
}
